<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\MenuRequest;
use App\Model\Menu;

class MenuController extends Controller
{

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function home(Request $request)
    {
		$data['code']=0;
		$data['data']=Menu::get();
		$data['error']="";
		$data['msg']="success";
		$data['timestamp']=time();
		return $data;
    }
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(MenuRequest $request)
    {
		$data=[
			'parent_id' => $request->parent_id,
			'name' => $request->name,
			'desc' => $request->desc,
			'prefix' => $request->prefix,
			'path' => $request->path,
			'icon' => $request->icon,
			'target' => $request->target,
			'order' => $request->order,
			'status' => $request->status,
		];

	   if($user=Menu::where("id",$request->id)->first()){	
		   $user->parent_id=$request->parent_id;
		   $user->name=$request->name;
		   $user->prefix=$request->prefix;
		   $user->path=$request->path;
		   $user->icon=$request->icon;
		   $user->target=$request->target;
		   $user->order=$request->order;
		   $user->status=$request->status;
		   $user->desc=$request->desc;
		   $user->save();
		   return ['code'=>0,"data"=>[],'msg'=>'修改成功'];
	   }

	   if(!$request->id){
			Menu::create($data);			
			return ['code'=>0,"data"=>[],'msg'=>'创建成功'];
	   }
	   return ['code'=>101,"data"=>[],'msg'=>'帐号已存在'];
    }	
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function delete(Request $request)
    {
		if(Menu::where("id",$request->id)->where("is_system",'<>',1)->delete()){
          return ['code'=>0,"data"=>1,'msg'=>'删除成功'];
		}else{
          return ['code'=>401,"data"=>0,'msg'=>'删除失败,你没有权限删除此用户'];
		}
    }
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function Action(Request $request,$id)
    {
		$data['code']=0;
		$data['data']=Menu::where('id',$id)->first()->access;
		$data['error']="";
		$data['msg']="success";
		$data['timestamp']=time();
		return $data;
    }
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function delAction(Request $request,$id)
    {
		Menu::where('id',$id)->first()->access()->detach($request->access_id);
		$data['code']=0;
		$data['data']=[];
		$data['error']="";
		$data['msg']="success";
		$data['timestamp']=time();
		return $data;
    }
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function storeAction(Request $request,$id)
    {
		Menu::where('id',$id)->first()->access()->syncWithoutDetaching($request->access_id);		
		$data['code']=0;
		$data['data']=[];
		$data['error']="";
		$data['msg']="success";
		$data['timestamp']=time();
		return $data;
    }
}
